package com.mystore.filter;

import com.mystore.action.ActionSupport;
import com.mystore.util.ParseXmlUtil;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

@WebFilter(filterName = "DispactherFilter", urlPatterns = {"*.do"},
        initParams = {
                @WebInitParam(name = "suffix", value = "do")
        })
public class DispactherFilter implements Filter {
    private String suffix;

    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;

        System.out.println("......");
        String uri = request.getRequestURI();
        int start = uri.lastIndexOf("/") + 1;
        int end = uri.lastIndexOf("."+suffix);
        String method = uri.substring(start, end);
        System.out.println("method =" + method);

        String classPath = new ParseXmlUtil().getAction(method);
        System.out.println("classpath=" + classPath);
        try {
            doAction(classPath, request);
        } catch (Exception e) {
            e.printStackTrace();
        }
        chain.doFilter(req, resp);
    }

    public void init(FilterConfig config) throws ServletException {
        suffix = config.getInitParameter("suffix");
    }

    public void doAction(String classPath, HttpServletRequest request) throws Exception {
        Object object = null;
        Class classzz = null;
        try {
            classzz = Class.forName(classPath);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        /**
         * 获取对象实例
         */
        object = classzz.newInstance();

        doFile(classzz, object, request);
        if (object instanceof ActionSupport) {

        } else {
            System.out.println("other method....");
            String methodName = "execute";
            Method m = null;
            try {
                m = classzz.getMethod(methodName, null);
                m.invoke(object, null);
            } catch (Exception e) {
                e.printStackTrace();
            }


        }

    }

    /**
     * 映射对象属性
     * @param clazz
     * @param object
     * @param request
     */
    public void doFile(Class clazz, Object object, HttpServletRequest request) {
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            //打印属性信息，包括访问控制修饰符，类型及属性名
            String name = field.getName();
            String methodName = "set" + name.substring(0, 1).toUpperCase() + name.substring(1);
            try {
                Method m = clazz.getMethod(methodName, String.class);
                System.out.println("111");
                m.invoke(object, request.getParameter(name));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


        /**
         * 获取对象的方法
         */
        Method[] methods = clazz.getDeclaredMethods();
        for (Method method : methods) {
            //打印方法签名
            System.out.println(method);

            System.out.println("修饰符：" + Modifier.toString(method.getModifiers()));
            System.out.println("方法名：" + method.getName());
            System.out.println("返回类型：" + method.getReturnType());
            //获取方法的参数对象
            Class<?>[] clazzes = method.getParameterTypes();
            for (Class<?> class1 : clazzes) {
                System.out.println("参数类型：" + class1);
            }
        }
    }


}
